package eventReservationServer.loginGateway;

import java.rmi.RemoteException;

import org.apache.axis2.AxisFault;

import eventReservationServer.loginGateway.proxies.AuthorizationWSInvalidPasswordExceptionException;
import eventReservationServer.loginGateway.proxies.AuthorizationWSInvalidUserExceptionException;
import eventReservationServer.loginGateway.proxies.AuthorizationWSStub;

public class AuthWSGateway implements IAuthorizeGateway { //TODO Ander Usar estos gateways

	/**
	 * Add your code to invoke the Authorization WS here Remember you must use
	 * the stub generated in the proxies package in order to invoke the WS. On
	 * the other hand, REMEMBER that you must catch the generic exception
	 * EXCEPTION AND SEE the contained message in order to map it to the
	 * ValidationException
	 * 
	 */
	public AuthWSGateway() { // TODO quito el parámetro "String serviceUri"

	}

	public UserDO login(String username, String pass) throws ValidationException {
		UserDO user = null;
		
		AuthorizationWSStub stub = null;
		try
		{
			stub = new AuthorizationWSStub("http://localhost:8080/axis2/services/AuthorizationWS");
		}
		catch (AxisFault e)
		{
			e.printStackTrace();
		}
		AuthorizationWSStub.User u = null;
		try
		{
			u = stub.login(username, pass);
		}
		catch (RemoteException e)
		{
			System.out.println("Error de validación: excepción remota."); // registro
			throw new ValidationException(
					"Error de validación: excepción remota.");
		}
		catch (AuthorizationWSInvalidUserExceptionException e)
		{
			System.out.println("Error de validación: usuario incorrecto."); // registro
			throw new ValidationException(
					"Error de validación: usuario incorrecto.");
		}
		catch (AuthorizationWSInvalidPasswordExceptionException e)
		{
			System.out.println("Error de validación: contraseña incorrecta."); // registro
			throw new ValidationException(
					"Error de validación: contraseña incorrecta.");
		}
		System.out.println("Name: " + u.getName() + " - Age: " + u.getAge() + " - Sex: " + u.getSex());
		
		user = new UserDO(u.getUsername(), u.getPassword(), u.getName(), u.getAge(), u.getSex());
	
		//TODO Pasar estas excepciones al servidor central
//		try {
//			AuthorizationWSStub stub = new AuthorizationWSStub("http://localhost:8080/axis2/services/AuthorizationWS");
//			AuthorizationWSStub.User  u = stub.login(username, pass);
//			System.out.println("Name: " + u.getName() + " - Age: " + u.getAge() + " - Sex: " + u.getSex());
//			
//			user = new UserDO(u.getUsername(), u.getPassword(), u.getName(), u.getAge(), u.getSex());
//		} catch (Exception e) {
//			System.out.println("Exception Type: " + e.getClass().getSimpleName());
//			if (e.getMessage().contains("[InvalidUserException]")) {
//				System.out.println("*** NON VALID USER ***");
//				System.out.println(e.getMessage());
//			}
//
//			if (e.getMessage().contains("[InvalidPasswordException]")) {
//				System.out.println("*** INVALID PASSWORD ***");
//				System.out.println(e.getMessage());
//			}
//		}
		
		return user;
	}
}